import request, { BASE_URL } from '../utils/request';

/**
 * 获取用户信息
 */
export function getUserInfo(userId) {
  return request({
    url: '/mini/user/info',
    method: 'GET',
    data: { userId }
  });
}

/**
 * 更新用户信息
 */
export function updateUserInfo(data) {
  return request({
    url: '/mini/user/info',
    method: 'PUT',
    data
  });
}

/**
 * 绑定学生信息
 */
export function bindStudentInfo(data) {
  return request({
    url: '/mini/user/bind-student',
    method: 'POST',
    data
  });
}

/**
 * 检查手机号是否存在
 */
export function checkPhoneExists(phone) {
  return request({
    url: '/mini/user/check-phone',
    method: 'GET',
    data: { phone }
  });
}

/**
 * 检查学号是否存在
 */
export function checkStudentIdExists(studentId) {
  return request({
    url: '/mini/user/check-student',
    method: 'GET',
    data: { studentId }
  });
}

/**
 * 注销账号
 */
export function deactivateUser(userId) {
  return request({
    url: '/mini/user/deactivate',
    method: 'POST',
    data: { userId }
  });
}

/**
 * 上传文件
 */
export function uploadFile(filePath, dir = 'avatar') {
  return new Promise((resolve, reject) => {
    const token = wx.getStorageSync('token');
    wx.uploadFile({
      url: `${BASE_URL}/mini/file/upload`,
      filePath: filePath,
      name: 'file',
      formData: {
        dir: dir
      },
      header: {
        'Authorization': token ? `Bearer ${token}` : ''
      },
      success: (res) => {
        try {
          const data = JSON.parse(res.data);
          if (data.code === 200) {
            resolve(data.data);
          } else {
            reject(new Error(data.message || '上传失败'));
          }
        } catch (error) {
          reject(error);
        }
      },
      fail: reject
    });
  });
} 